#!/bin/bash
# dicomedit_kidneys modifies metatdata in DICOM 
# files for the kidney project. Origninal files produced by the Agilent 9.4T
# MR scanner were not accepted by the DaRIS system.  This script roughly 
# fixes this problem.
#
#   - Michael Eager,  (michael.eager@monash.edu)
#   - Monash Biomedical Imaging,  Monash University, Australia

#     Copyright © 2012-2013 Michael Eager <michael.eager@monash.edu> 
#
#     This file is part of Xglom.
# 
#     This is free software: you can redistribute it and/or modify
#     it under the terms of the GNU General Public License as published by
#     the Free Software Foundation, either version 3 of the License, or
#     (at your option) any later version.
# 
#     This is distributed in the hope that it will be useful,
#     but WITHOUT ANY WARRANTY; without even the implied warranty of
#     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#     GNU General Public License for more details.
# 
#     You should have received a copy of the GNU General Public License
#     along with this program.  If not, see <http://www.gnu.org/licenses/>.


. dcheck
. ~/.daris_current_parameters
E_BADARGS=65

if [ $# -eq 0 ]
then
  echo "Usage: `basename $0` input.dcm [output.dcm]"
  exit $E_BADARGS
fi



if [ $# -gt 1 ]
then
    infilename="${1}"
    if [ ! -f "${infilename}" ];then
	echo "Input argument is not a valid file"
	exit 1
    fi
    if [ $# -eq 0 ]
    then
	outfilename="${2}"
    else
	outfilename=""
    fi
    if [ "${outfilename}" == "" ];then
	echo "Output argument is not valid.  Overwriting input DICOM."
	outfilename="${infilename}"
    else
	cp "${infilename}" "${outfilename}"
    fi

fi


echo "${infilename}"
sequence=$(./dcmtools/bin/DCMDUMP ${filename} | awk '/SequenceName/ {print $5}' |tr -d '><')
echo $sequence

./dcmtools/bin/DCMODIFY "(0008,103E)='Kidney image-${sequence}'" ${outfilename}
./dcmtools/bin/DCMODIFY "(0018,1030)=${filename}" ${outfilename}
